<?php

// $Id$


require_once(TANGRA_MAIN_DIR.'db/paginate_db_data.class.php');
require_once(TANGRA_MAIN_DIR.'grids/grid_ctrl.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/grids/vpage_select/vpage_select_grid.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/tcms_ci_type_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_ci_dbc.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_system_attribute_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_system_attribute_dbc.class.php');



class VPage_Select_Grid_Ctrl extends Grid_Ctrl {
	function __construct($system_name, Vars_Manager $vm, $method = Tangra_Parameter_Method::GET) {
		$this->set_user_defined_post_processing_method('postprocessing');

		parent::__construct($system_name, $vm, $method);
	}


	protected function postprocessing(DB_Connection $dbc, $rows) {
		foreach($rows as $key => &$row) {
			$row[7] = $this->is_missing($dbc, $row[0]);
		}

		return $rows;
	}


	protected function create_grid() {
		$ret = new vpage_select_Grid();

		return $ret;
	}


	protected function create_paginate_db_data() {
		$sqls = '';
		$sqlc = '';

		$pdbd = new Paginate_DB_Data($this->grid->get_page_size(), $sqls, $sqlc);

		return $pdbd;
	}


	public function process(DB_Connection $dbc, $page = NULL) {
		$vm = $this->get_vm();

		$sql = $this->get_sqls($vm);

		$this->paginate_db_data->set_sql_select($sql['sqls']);
		$this->paginate_db_data->set_sql_count($sql['sqlc']);

		parent::process($dbc, $page);
	}


	private function get_sqls($vm) {
		$path = $vm->get_page_var('vpage_select', 'path');
		$online = $vm->get_page_var('vpage_select', 'online');
		$template = $vm->get_page_var('vpage_select', 'template');
		$sys_gen = $vm->get_page_var('vpage_select', 'sys_gen');

		$sqls = TCMS_VPage_DBC::get_sql_for_grid($path, $online, $template, $sys_gen);
		$sqlc = TCMS_VPage_DBC::get_sql_count_for_grid($path, $online, $template, $sys_gen);

		$sql['sqls'] = $sqls;
		$sql['sqlc'] = $sqlc;

		return $sql;
	}


	private function is_missing(DB_Connection $dbc, $vpage_id) {
		$ret = 0;

		$vpage = new TCMS_VPage_DBC();
		$vpage->load_by_id($dbc, $vpage_id);

		$vp_template = new TCMS_VPage_Template_DBC();
		$vp_template->load_by_id($dbc, $vpage->get_template());



		$t_sas = TCMS_VPT_System_Attribute_DBC::select_vpt_sas_for_template($dbc, $vpage->get_template());
		$page_system_attributes = TCMS_VPage_System_Attribute_DBC::load_all_for_vpage($dbc, $vpage_id);
		$missing_required_sa = false;
		foreach($t_sas as $key => &$tas) {
			if (!array_key_exists($tas['id'], $page_system_attributes)) {
				$ret = 1;
				if ($tas['required']) {
					$ret = 2;
				}
			}
		}

		if ($ret < 2) {
			$t_cis = TCMS_CI_Type_DBC::load_ci_types_for_template($dbc, $vp_template->get_id());
			$vp_cis = TCMS_VPage_CI_DBC::load_cis_for_page($dbc, $vpage_id);
			$missing_required_ci = false;
			foreach($t_cis as $key => &$tis) {
				if (!array_key_exists($tis['id'], $vp_cis)) {
					$ret = 1;
					if ($tis['required']) {
					$ret = 2;
					}
				}
			}
		}


		return $ret;
	}
}